--
-- Author: shunguo.chen
-- Date: 2016-10-28 10:47:18
--
-- MallConfirmView

local NumInputView = require("app.widget.NumInputView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.MallConfirmScene

-- data = MallVo
function ClassRef:init()
	self._curNum = nil
	self._maxNum = nil
	self._gift = 0

	local btnCon = self._ui.shop_tips

	self._priceText = btnCon:findChild("price/coin_whole")
	self._totalText = btnCon:findChild("total/coin_whole")

	self._numText = btnCon:findChild("01Nun/04Num")
	self._numTip = btnCon:findChild("01Nun/tips")
	self._subBtn = btnCon:findChild("01Nun/02Button_add")
	self._addBtn = btnCon:findChild("01Nun/03Button_sub")
	self._numBtn = btnCon:findChild("01Nun/05Btn_num")
	self._subBtn:addClickEventListener(function()
			self:numChange(self._curNum - 1)
		end)
	self._addBtn:addClickEventListener(function()
			self:numChange(self._curNum + 1)
		end)
	self._numBtn:addClickEventListener(function()
			self:openNumPanel()
		end)

	self._ui.mask:setTouchEnabled(true)
	self._ui.mask:addClickEventListener(function()
			executeFunc(self._onCloseView)
		end)
	self._ui.Button_cancel:addClickEventListener(function()
			executeFunc(self._onCloseView)
		end)
	self._ui.Button_buy:addClickEventListener(function()
			if self._data:getBuyNumMax() > 0 then
				if self._moneyEnough then
					executeFunc(self._onCloseView)
				else
					display.pushToast(L("tips_2000225"))
				end
			else
				display.pushToast(L("tips_2000224"))
			end
		end)

	self:refresh()
end

function ClassRef:remove()
	self.super.remove(self)
	UIHandler:hideFloatPanel()
end

function ClassRef:refresh()
	self._priceText:setString(self._data.moneyCost)

	self:showTip()

	self:numChange(1)
end

function ClassRef:showTip()
	local data = self._data:getItemData()
	local type = data.type

	local param = {itemId=tonumber(data.tid)}
	local tipsType
	if type == Enums.ItemBagType.daibi then
		tipsType = Enums.TipsType.daibi
		param.itemId = tonumber(data.tid)
	elseif type == Enums.ItemBagType.mount then
		tipsType = Enums.TipsType.mount
	elseif type == Enums.ItemBagType.equip then
		tipsType = Enums.TipsType.equip
		param.dataTemp = data
	elseif type == Enums.ItemBagType.jewel then
		tipsType = Enums.TipsType.jewel
		param.uid = data.uid
		param.dataTemp = data
	else
		tipsType = Enums.TipsType.item
	end

	param.tipsType = tipsType
	app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params=param})
end

function ClassRef:numChange(v)
	local max = self._data:getBuyNumMax()
	v = math.min(v, max)
	v = math.max(v, 1)

	self._maxNum = max
	if nil == self._curNum then
		self._curNum = v
	elseif self._curNum ~= v then
		self._curNum = v
		executeFunc(self._onOperate, "numChange", self._curNum * self._data.moneyCost)
	elseif self._curNum == v then
		return
	end

	display.setBtnTouchEnable(self._subBtn, v > 1)
	display.setBtnTouchEnable(self._addBtn, v < max)
	self._numBtn:setTouchEnabled(max > 1)
	self._numTip:setVisible(max > 1)

	self._numText:setString(self._curNum)
	local total = self._curNum * self._data.moneyCost - self._gift
	local own = UD:getDaibiValue(self._data.moneyId)
	self._moneyEnough = (own >= total)
	self._totalText:setString(total)
	self._totalText:setColor(self._moneyEnough and display.COLOR_WHITE or display.COLOR_RED)
end

function ClassRef:openNumPanel()
	local size = self._numBtn:getContentSize()
	local pos = self._numBtn:convertToWorldSpace(cc.p(size.width/2, size.height))
	pos.x = pos.x - 200
	local inputView = NumInputView.new(pos, 0, self._maxNum, 2, function(v)
			if v > 0 then
				self:numChange(v)
			end
		end)
    self:addChild(inputView)
end

return ClassRef
